/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package post;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.text.*;
/**
 *
 * @author sean
 */
public class Transaction {
    private ArrayList<Item> items = new ArrayList<Item>();
    //private Payment pay;
    private double totalCost = 0;
    private Payment pay;
    /**
     *
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void setTransactionFromFile() throws FileNotFoundException, IOException
    {
        TransactionReader reader = new TransactionReader("Store//transactions.txt");
        //name = reader.getName();    //retrieve customers name from file
        while( reader.hasMoreTrans() )
        {
            Item tempItem = reader.getNextItem();
            if( tempItem != null )  //if more items need to be added
            {
                items.add(tempItem); //add next item to list
            }
            else
            {
                pay = reader.getPayment();  //retrieve payment from file
                break;
            }
        }
    }//end setTransationFromFile

    /**
     *
     * @param cat
     * @param upc
     * @param amt
     */
    public void addItem( Catalog cat, String upc, int amt )//Catalog parameter will be removed once catalog is fixed
    {
        Item tempItem = new Item(upc, amt); //instantiate item to be added
        tempItem.setDecription(cat.getSpec(upc).getDescription());  //add item description from catalog
        tempItem.setPrice(cat.getSpec(upc).getPrice()); //add item price from catalog
        items.add(tempItem);    //add item to the list
        totalCost += cat.getSpec(upc).getPrice();   //update current total cost
    }//end additem

    /**
     *
     * @param upc
     */
    public void removeItem( String upc )    //remove entire instance of item
    {
        Iterator it = items.iterator();
        while( it.hasNext() )
        {
            Item tempItem = (Item) it.next();   //iterate next item
            if( tempItem.getUpc().equals(upc))  //found the matching item
            {
                it.remove();    //remove item from list
            }
        }
    }//end removeItem

    /**
     *
     * @param upc
     * @param amt
     */
    public void setItemAmt( String upc, int amt )   //set certain amt of this item
    {
        Iterator it = items.iterator();
        while( it.hasNext() )
        {
            Item tempItem = (Item) it.next();   //iterate next item
            if( tempItem.getUpc().equals(upc))  //found the matching item
            {
                tempItem.setQty(amt);   //change amount of this item
            }
        }
    }//end removeItem

    /**
     *
     * @param p
     * Set payment to be used for transaction
     */
    public double pay( Payment p )
    {
        return p.makePayment(totalCost);
    }
    
    /**
    *
    * @return
    */
    public double getTotal()    //get total cost of items
    {
        return totalCost;
    }

    /**
     *
     * @param cat
     */
    public void printInvoice( Catalog cat ) //The parameter should be blank once the Catalog class is fixed
    {
        NumberFormat nf = new DecimalFormat("#0.00");   //fixed format for monetary values
        
        System.out.println("\tTraveling Baker\n");  //prints store name
        System.out.println(new Date()); //prints time of transaction
        Iterator it = items.iterator();
        while( it.hasNext() )
        {
            Item tempItem = (Item) it.next();   //iterate through list of items purchased
            ProductSpec tempSpec = cat.getSpec( tempItem.getUpc() );    //find matching product info
            if (tempSpec == null) System.out.println("Invalid UPC -- No Such Item");    //if upc lookup failed
            System.out.println( tempSpec.getDescription() + " <" + tempItem.getQty() + " @ "
                    + String.format("%6s", nf.format(tempSpec.getPrice())) + ">\t"
                    + String.format("%6s", nf.format(tempSpec.getPrice() * tempItem.getQty())) );//prints all items purchased
            totalCost += tempSpec.getPrice() * tempItem.getQty();   //calculate total cost
        }
        System.out.println("-----");    //separates items from payment info
        System.out.println("Total $" + nf.format(totalCost));   //amount expected to pay
        /*if( pay.getPaymentType().equals("CASH") )
            System.out.println("Amount Tendered: " + nf.format(pay.getPayment()));  //amount paid by customer
        else System.out.println( pay.getPaymentType() );
        {
            if( pay.getPayment() >= 0 ) //if payment is enough
                System.out.println("Amout Returned: " + nf.format(pay.makePayment(totalCost)));//change returned to customer
            else
                System.out.println("Transaction Cancelled -- Could Not Authorize");  //if payment fails
        }*/
    }//end printInvoice
}
